<?php
declare(strict_types=1);

namespace app\process;

use Workerman\Connection\TcpConnection;

// 实验性websocket服务
class CanvasWebsocket
{
    public function onConnect(TcpConnection $connection)
    {
        echo "onConnect\n";
    }

    public function onWebSocketConnect(TcpConnection $connection, $http_buffer)
    {
        echo "onWebSocketConnect\n";
    }

    public function onMessage(TcpConnection $connection, $data)
    {

        foreach ($connection->worker->connections as $_connection) {
            if ($connection != $_connection) {
                $_connection->send($data);
            }
        }
    }

    public function onClose(TcpConnection $connection)
    {
        echo "onClose\n";
    }
}